<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */
$basedir = dirname(__FILE__);

require_once $basedir . '/../../../../classes/mm/xrd.php';
require_once $basedir . '/../../../../classes/mm/atom.php';

require_once $basedir . '/../../../../classes/mm/xml/element/builder/interface.php';
require_once $basedir . '/../../../../classes/mm/xml/element/builder/abstract.php';
require_once $basedir . '/../../../../classes/mm/xml/element/abstract.php';
require_once $basedir . '/../../../../classes/mm/xml/element/parent.php';
require_once $basedir . '/../../../../classes/mm/xml/element/parser.php';

require_once $basedir . '/../../../../classes/mm/xrd/element/lrdd.php';
require_once $basedir . '/../../../../classes/mm/xrd/element/alias.php';
require_once $basedir . '/../../../../classes/mm/xrd/element/subject.php';
require_once $basedir . '/../../../../classes/mm/xrd/element/link.php';
require_once $basedir . '/../../../../classes/mm/xrd/element/lrdd/builder.php';

class Mm_Xrd_Element_LrddTest extends PHPUnit_Framework_TestCase {

    public function testParsingLrddFromIdentica () {
        $rawXML = '<?xml version="1.0" encoding="UTF-8"?>
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
  <Subject>acct:testkoski@identi.ca</Subject>
  <Alias>http://identi.ca/user/209172</Alias>
  <Link rel="http://webfinger.net/rel/profile-page" type="text/html" href="http://identi.ca/user/209172"/>
  <Link rel="http://schemas.google.com/g/2010#updates-from" href="http://identi.ca/api/statuses/user_timeline/209172.atom" type="application/atom+xml"/>
  <Link rel="http://microformats.org/profile/hcard" type="text/html" href="http://identi.ca/testkoski/hcard"/>
  <Link rel="http://gmpg.org/xfn/11" type="text/html" href="http://identi.ca/user/209172"/>
  <Link rel="describedby" type="application/rdf+xml" href="http://identi.ca/testkoski/foaf"/>
  <Link rel="salmon" href="http://identi.ca/main/salmon/user/209172"/>
  <Link rel="http://salmon-protocol.org/ns/salmon-replies" href="http://identi.ca/main/salmon/user/209172"/>
  <Link rel="http://salmon-protocol.org/ns/salmon-mention" href="http://identi.ca/main/salmon/user/209172"/>
  <Link rel="magic-public-key" href="data:application/magic-public-key,RSA.hNo0yCdfxmx2oFRQQ7J2Qps8b9DEoGL6MEoYSBIt1JXC08Wpl_D1BiYMRtkOvfoB9h1xJA-yoYZZAYDch3UR-XzNy2PLxvDUFOXaMguyTTTF6x4cY9HWZF71iQo1oN5x5C3PSOxiKNTZ1-IYCMdInszr_oFWbbFuDg77VqfpNMk=.AQAB"/>
  <Link rel="http://ostatus.org/schema/1.0/subscribe" template="http://identi.ca/main/ostatussub?profile={uri}"/>
</XRD>';
        $builder = new Mm_Xrd_Element_Lrdd_Builder($rawXML);
        $lrdd = $builder->build();
        
        $this->assertEquals("acct:testkoski@identi.ca", $lrdd->getSubject()->value);
        $this->assertEquals("http://identi.ca/user/209172", $lrdd->getAlias()->value);
     
        $this->assertEquals("http://webfinger.net/rel/profile-page", $lrdd->getWebfingerProfilepageLink()->rel);
        $this->assertEquals("http://identi.ca/user/209172", $lrdd->getWebfingerProfilepageLink()->href);
        $this->assertEquals("text/html", $lrdd->getWebfingerProfilepageLink()->type);
        
        $this->assertEquals("http://schemas.google.com/g/2010#updates-from", $lrdd->getUpdatesFromLink()->rel);
        $this->assertEquals("http://identi.ca/api/statuses/user_timeline/209172.atom", $lrdd->getUpdatesFromLink()->href);
        $this->assertEquals("application/atom+xml", $lrdd->getUpdatesFromLink()->type);
        
        $this->assertEquals("salmon", $lrdd->getSalmonLink()->rel);
        $this->assertEquals("http://identi.ca/main/salmon/user/209172", $lrdd->getSalmonLink()->href);
        
        $this->assertEquals("http://salmon-protocol.org/ns/salmon-replies", $lrdd->getSalmonRepliesLink()->rel);
        $this->assertEquals("http://identi.ca/main/salmon/user/209172", $lrdd->getSalmonRepliesLink()->href);
        
        $this->assertEquals("http://salmon-protocol.org/ns/salmon-mention", $lrdd->getSalmonMentionLink()->rel);
        $this->assertEquals("http://identi.ca/main/salmon/user/209172", $lrdd->getSalmonMentionLink()->href);
        
        $this->assertEquals("magic-public-key", $lrdd->getMagicPublicKeyLink()->rel);
        $this->assertEquals("data:application/magic-public-key,RSA.hNo0yCdfxmx2oFRQQ7J2Qps8b9DEoGL6MEoYSBIt1JXC08Wpl_D1BiYMRtkOvfoB9h1xJA-yoYZZAYDch3UR-XzNy2PLxvDUFOXaMguyTTTF6x4cY9HWZF71iQo1oN5x5C3PSOxiKNTZ1-IYCMdInszr_oFWbbFuDg77VqfpNMk=.AQAB", $lrdd->getMagicPublicKeyLink()->href);
        
        $expected = '<?xml version="1.0" encoding="UTF-8"?>
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
  <Subject>acct:testkoski@identi.ca</Subject>
  <Alias>http://identi.ca/user/209172</Alias>
  <Link rel="salmon" href="http://identi.ca/main/salmon/user/209172"/>
  <Link rel="http://salmon-protocol.org/ns/salmon-replies" href="http://identi.ca/main/salmon/user/209172"/>
  <Link rel="http://salmon-protocol.org/ns/salmon-mention" href="http://identi.ca/main/salmon/user/209172"/>
  <Link rel="http://schemas.google.com/g/2010#updates-from" type="application/atom+xml" href="http://identi.ca/api/statuses/user_timeline/209172.atom"/>
  <Link rel="http://webfinger.net/rel/profile-page" type="text/html" href="http://identi.ca/user/209172"/>
  <Link rel="magic-public-key" href="data:application/magic-public-key,RSA.hNo0yCdfxmx2oFRQQ7J2Qps8b9DEoGL6MEoYSBIt1JXC08Wpl_D1BiYMRtkOvfoB9h1xJA-yoYZZAYDch3UR-XzNy2PLxvDUFOXaMguyTTTF6x4cY9HWZF71iQo1oN5x5C3PSOxiKNTZ1-IYCMdInszr_oFWbbFuDg77VqfpNMk=.AQAB"/>
</XRD>
';
        
        $this->assertEquals($expected, $lrdd->asXML());
    }
    
    public function testParsingLrddFromCliqset () {
        $rawXML = "<?xml version='1.0'?>
<XRD xmlns='http://docs.oasis-open.org/ns/xri/xrd-1.0'>
	<Subject>acct:cnnbrk@cliqset.com</Subject>
	<Alias>http://cliqset.com/user/cnnbrk</Alias>
	<Link rel='http://microformats.org/profile/hcard' type='text/html' href='http://cliqset.com/etc/hcard/cnnbrk'/>
	<Link rel='http://webfinger.net/rel/profile-page' href='http://cliqset.com/user/cnnbrk' type='text/html'/>
	<Link rel='describedby' href='http://cliqset.com/user/cnnbrk' type='text/html'/>
	<Link rel='http://schemas.google.com/g/2010#updates-from' href='http://cliqset.com/feed/atom?uid=cnnbrk' type='application/atom+xml'/>
	<Link rel='magic-public-key' href='data:application/magic-public-key,RSA.sG1aDqcAD3rZNOb0QsULHa7gMfPbh6sJ0aLEnmgyF3CKLPcFar1XrF13q6c8ci4eIrZqbvZpoa1NlCTyT5MTnSdis1b7h7_Jjtp1hB0PHwTp-lxqLIpTeNOZz5cLO_8q5sCtSmQ7p07s5CddJg4ku-GU-826MV8j7PpZUFCh3lE=.AQAB' />
	<Link rel='http://salmon-protocol.org/ns/salmon-mention' href='http://cliqset.com/etc/salmon/mentions/cnnbrk' />	<Link rel='http://salmon-protocol.org/ns/salmon-replies' href='http://cliqset.com/etc/salmon/replies/cnnbrk' /></XRD>";
        $builder = new Mm_Xrd_Element_Lrdd_Builder($rawXML);
        $lrdd = $builder->build();
        
        $this->assertEquals("acct:cnnbrk@cliqset.com", $lrdd->getSubject()->value);
        $this->assertEquals("http://cliqset.com/user/cnnbrk", $lrdd->getAlias()->value);
     
        $this->assertEquals("http://webfinger.net/rel/profile-page", $lrdd->getWebfingerProfilepageLink()->rel);
        $this->assertEquals("http://cliqset.com/user/cnnbrk", $lrdd->getWebfingerProfilepageLink()->href);
        $this->assertEquals("text/html", $lrdd->getWebfingerProfilepageLink()->type);
        
        $this->assertEquals("http://schemas.google.com/g/2010#updates-from", $lrdd->getUpdatesFromLink()->rel);
        $this->assertEquals("http://cliqset.com/feed/atom?uid=cnnbrk", $lrdd->getUpdatesFromLink()->href);
        $this->assertEquals("application/atom+xml", $lrdd->getUpdatesFromLink()->type);
        
        //$this->assertEquals("salmon", $lrdd->getSalmonLink()->rel);
        //$this->assertEquals("http://identi.ca/main/salmon/user/209172", $lrdd->getSalmonLink()->href);
        
        $this->assertEquals("http://salmon-protocol.org/ns/salmon-replies", $lrdd->getSalmonRepliesLink()->rel);
        $this->assertEquals("http://cliqset.com/etc/salmon/replies/cnnbrk", $lrdd->getSalmonRepliesLink()->href);
        
        $this->assertEquals("http://salmon-protocol.org/ns/salmon-mention", $lrdd->getSalmonMentionLink()->rel);
        $this->assertEquals("http://cliqset.com/etc/salmon/mentions/cnnbrk", $lrdd->getSalmonMentionLink()->href);
        
        $this->assertEquals("magic-public-key", $lrdd->getMagicPublicKeyLink()->rel);
        $this->assertEquals("data:application/magic-public-key,RSA.sG1aDqcAD3rZNOb0QsULHa7gMfPbh6sJ0aLEnmgyF3CKLPcFar1XrF13q6c8ci4eIrZqbvZpoa1NlCTyT5MTnSdis1b7h7_Jjtp1hB0PHwTp-lxqLIpTeNOZz5cLO_8q5sCtSmQ7p07s5CddJg4ku-GU-826MV8j7PpZUFCh3lE=.AQAB", $lrdd->getMagicPublicKeyLink()->href);
        
        $expected = '<?xml version="1.0" encoding="UTF-8"?>
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
  <Subject>acct:testkoski@identi.ca</Subject>
  <Alias>http://identi.ca/user/209172</Alias>
  <Link rel="salmon" href="http://identi.ca/main/salmon/user/209172"/>
  <Link rel="http://salmon-protocol.org/ns/salmon-replies" href="http://identi.ca/main/salmon/user/209172"/>
  <Link rel="http://salmon-protocol.org/ns/salmon-mention" href="http://identi.ca/main/salmon/user/209172"/>
  <Link rel="http://schemas.google.com/g/2010#updates-from" type="application/atom+xml" href="http://identi.ca/api/statuses/user_timeline/209172.atom"/>
  <Link rel="http://webfinger.net/rel/profile-page" type="text/html" href="http://identi.ca/user/209172"/>
  <Link rel="magic-public-key" href="data:application/magic-public-key,RSA.hNo0yCdfxmx2oFRQQ7J2Qps8b9DEoGL6MEoYSBIt1JXC08Wpl_D1BiYMRtkOvfoB9h1xJA-yoYZZAYDch3UR-XzNy2PLxvDUFOXaMguyTTTF6x4cY9HWZF71iQo1oN5x5C3PSOxiKNTZ1-IYCMdInszr_oFWbbFuDg77VqfpNMk=.AQAB"/>
</XRD>
';
        
        $this->assertEquals($expected, $lrdd->asXML());
    }
}